#include "myform.h"
#include "ui_myform.h"

MyForm::MyForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MyForm)
{
    ui->setupUi(this);
    init();
}

MyForm::~MyForm()
{
    delete ui;
}

bool MyForm::eventFilter(QObject *obj, QEvent *event)
{
    switch (event->type()) {
    case QEvent::FocusIn:
        for(int i=1;i<=5;++i){
            QLineEdit* edit = findChild<QLineEdit*>((QString("line_%1").arg(i)));
            if(edit == obj){ // 显示当前的editLab，隐藏其他editLab
                hideEditLab();
                QLabel* lab = findChild<QLabel*>(QString("editLab_%1").arg(i));
                if(lab!=nullptr){
                    lab->show();
                }else{
                    qDebug() << QString("error editLab_%1").arg(i);
                }
                break;
            }
        }
        break;
    default:
        break;
    }
    return QWidget::eventFilter(obj,event);
}

void MyForm::init()
{
    hideEditLab();
    for(int i=1;i<=5;++i){
        QLineEdit* edit = findChild<QLineEdit*>(QString("line_%1").arg(i));
        if(edit!=nullptr){
            edit->installEventFilter(this);
        }else{
            qDebug() << QString("error edit_%1").arg(i);
        }
    }
}

void MyForm::hideEditLab()
{
    for(int i=1;i<=5;++i){
        QLabel* lab = findChild<QLabel*>(QString("editLab_%1").arg(i));
        if(lab!=nullptr){
            lab->hide();
        }else{
            qDebug() << QString("error editLab_%1").arg(i);
        }
    }
}
